package com.hoow.shakoow.shake

import android.annotation.SuppressLint
import android.os.*
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hoow.shakoow.ai.data.RevObjUserAblum
import com.hoow.shakoow.ai.home.NetworkViewModel
import com.hoow.shakoow.base.BaseActivity
import com.hoow.shakoow.databinding.ActivityUserAblumBinding
import com.hoow.shakoow.enums.PageTagEnum
import com.hoow.shakoow.extend.*
import com.hoow.shakoow.shake.adapter.UserAblumAdapter
import com.hoow.shakoow.shake.components.imageView.ShakeImageGallary
import com.hoow.shakoow.utils.analytics.util.AnalyticsUtil


/** 相册列表页 */
@SuppressLint("CustomSplashScreen")
class UserAblumActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityUserAblumBinding

    // 适配器
    private lateinit var mUserAblumAdapter: UserAblumAdapter

    //api接口viewModel
    private val mNetworkViewModel by lazy {
        ViewModelProvider(this)[NetworkViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mNetworkViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.user_photos.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivityUserAblumBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n", "SourceLockedOrientationActivity", "NotifyDataSetChanged")
    override fun onContentViewBinded() {
        try {
            // 初始化适配器
            UserAblumAdapter.initItemSize(this)

            mNetworkViewModel.mUserAblumList.clear()
            ShakeUserManager.mCurShakeUserInfo?.let { user ->
                user.userPhotos?.let { result ->
                    mNetworkViewModel.mUserAblumList.addAll(result)
                }
            }

            mUserAblumAdapter = UserAblumAdapter(false, mNetworkViewModel.mUserAblumList)
            mUserAblumAdapter.setItemClickListener { i, avatarLink, isDelete ->
                //点击图片
                ShakeImageGallary.showDialog(
                    fragmentManager = supportFragmentManager,
                    dataList = mNetworkViewModel.mUserAblumList,
                    index = i
                )

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "SHOW_PHOTO_GALLARY",
                    remark = ""
                )
            }

            mActivityBinding.rcAblumList.layoutManager =
                StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)
            mActivityBinding.rcAblumList.adapter = mUserAblumAdapter

            // 更新相册
            val userPhotosArray =
                intent.getStringArrayListExtra("userPhotos") ?: ArrayList<String>()

//            mNetworkViewModel.mUserAblumList.clear()
//            userPhotosArray.forEach { url ->
//                if (url.isNotBlank()) {
//                    mNetworkViewModel.mUserAblumList.add(RevObjUserAblum(id = "0", imgUrl = url))
//                }
//            }

            // 更新列表
            mUserAblumAdapter.notifyDataSetChanged()

            // 更新空白页展示
            if (mNetworkViewModel.mUserAblumList.size > 0) {
                mActivityBinding.emptyLayout.llEmptyList.visibility = View.GONE
            } else {
                mActivityBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}